package com.imooc.controller;

import com.imooc.pojo.UserAddress;
import com.imooc.pojo.bo.AddressBO;
import com.imooc.service.AddressService;
import imooc.utils.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "地址接口", tags = {"地址信息展示的相关接口"})
@RestController
@RequestMapping("address")
public class AddressController extends BaseController{

    @Autowired
    private AddressService addressService;

    /**
     *
     * 用户在确认订单页面，可以针对收获地址做如下操作
     * 1.查询用户的所有地址
     * 2.新增
     * 3，删除
     * 4.修改
     * 5.设置默认
     **/

    @ApiOperation(value = "根据用户ID，查询收货地址列表", notes = "根据用户ID，查询收货地址列表", httpMethod = "GET")
    @PostMapping("/list")
    public ApiResponse list(
            @ApiParam(name = "userId", value = "用户ID",required = true, example = "1")
            @RequestParam String userId) throws Exception {
        if (StringUtils.isBlank(userId)) {
            return ApiResponse.errorMsg("");
        }
        List<UserAddress> userAddressList = addressService.queryAll(userId);
        return ApiResponse.ok(userAddressList);
    }

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/28 15:29
     * @description    添加地址
     **/
    @ApiOperation(value = "添加地址", notes = "添加地址", httpMethod = "POST")
    @PostMapping("/add")
    public ApiResponse add(@RequestBody AddressBO addressBO) throws Exception {
        addressService.addNewUserAddress(addressBO);
        return ApiResponse.ok("添加成功");
    }

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/28 15:29
     * @description    修改地址
     **/
    @ApiOperation(value = "修改地址", notes = "修改地址", httpMethod = "POST")
    @PostMapping("/update")
    public ApiResponse update(@RequestBody AddressBO addressBO) throws Exception {
        if (StringUtils.isBlank(addressBO.getAddressId())) {
            return ApiResponse.errorMsg("addressId不能为空");
        }
        addressService.updateNewUserAddress(addressBO);
        return ApiResponse.ok();
    }

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/28 15:30
     * @description   删除地址
     **/
    @ApiOperation(value = "删除地址", notes = "删除地址", httpMethod = "POST")
    @PostMapping("/delete")
    public ApiResponse delete(
            @ApiParam(name = "userId", value = "用户ID",required = true, example = "1")
            @RequestParam String userId,
            @ApiParam(name = "addressId", value = "地址ID",required = true, example = "1")
            @RequestParam String addressId) throws Exception {
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(addressId) ) {
            return ApiResponse.errorMsg("");
        }
        addressService.deleteNewUserAddress(userId, addressId);
        return ApiResponse.ok();
    }

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/28 15:27
     * @description    设置默认地址
     **/
    @ApiOperation(value = "设置默认地址", notes = "设置默认地址", httpMethod = "GET")
    @PostMapping("/setDefalut")
    public ApiResponse setDefalut(
            @ApiParam(name = "userId", value = "用户ID",required = true, example = "1")
            @RequestParam String userId,
            @ApiParam(name = "addressId", value = "地址ID",required = true, example = "1")
            @RequestParam String addressId) throws Exception {
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(addressId) ) {
            return ApiResponse.errorMsg("");
        }
        addressService.updateUserAddressToBeDefault(userId, addressId);
        return ApiResponse.ok();
    }


}
